package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg,omitempty"`
	Data T      `json:"data,omitempty"`
}

func Result[T any](c *gin.Context, httpStatusCode int, code int, msg string, data T) {
	// 开始时间
	c.JSON(httpStatusCode, Response[T]{
		Code: code,
		Msg:  msg,
		Data: data,
	})
}

// Success basic with default msg and data is empty
func Success(c *gin.Context, code int) {
	Result(c, http.StatusOK, code, "success", map[string]interface{}{})
}

// SuccessWithMsg success with msg and data is empty
func SuccessWithMsg(c *gin.Context, code int, msg string) {
	Result(c, http.StatusOK, code, msg, map[string]interface{}{})
}

// SuccessWithDetail  success with msg and data
func SuccessWithDetail[T any](c *gin.Context, code int, msg string, data T) {
	Result(c, http.StatusOK, code, msg, data)
}

// Fail  with default msg and data is empty
func Fail(c *gin.Context, httpStatusCode int, code int) {
	Result(c, httpStatusCode, code, "fail", map[string]interface{}{})
}

func FailWithMsg(c *gin.Context, httpStatusCode int, code int, msg string) {
	Result(c, httpStatusCode, code, msg, map[string]interface{}{})
}

func FailWithDetail[T any](c *gin.Context, httpStatusCode int, code int, msg string, data T) {
	Result(c, httpStatusCode, code, msg, data)
}
